import FileUtils as fu
from ClickEntity import ClickEntity
from Position import Position

fileUtils = fu.FileUtils()

# select querycode,qid,featureline,position,click from dw_globalsearchdb.gl_around_sort_feature
lines = fileUtils.readLine(r'D:\Users\yincy\Desktop\新建文件夹\000000_0')

# querycode :
dic = {}
for line in lines:
    row = line.split('\001')
    querycode = row[0]
    qid = row[1]
    featuereline = row[2]
    position = row[3]
    click = row[4]
    isclick = 0
    if '1' == click:
        isclick = 1
    pos = Position(qid, featuereline, position, isclick)
    if querycode in dic:
        clickEntity1 = dic[querycode]
        clickEntity1.position.append(pos)
    else:
        clickEntity2 = ClickEntity(querycode)
        clickEntity2.position.append(pos)
        dic[querycode] = clickEntity2

reslist = []

# 打印dic
for key, value in dic.items():
    value.doRank()
    querycode = value.querycode
    value_positions = value.position
    for index, value_position in enumerate(value_positions):
        # str1 = '{}\t{}\t{}\t{}'.format(querycode, value_position.featureline, value_position.position,  value_position.click)
        # res = '{} {}\t{}\t{}'.format((10 - index), value_position.featureline, value_position.position, value_position.click)
        res = '{} qid:{} {} {}'.format((10 - index), value_position.qid, value_position.featureline, value_position.position)
        # print(str)
        print(res)
        reslist.append(res)
        # print(querycode + '\t' + value_position.featureline + '\t' +value_position.position+ '\t' +value_position.click)

fileUtils.writeLine(r'D:\Users\yincy\Desktop\新建文件夹\res.txt', reslist)
